<?php
/**
 * Created by jingjingxyk.
 * User: yaokun
 * Date: 17-8-1
 * Time: 下午9:51
 */

namespace ContentOutputBundle\Services;

use Symfony\Component\Yaml\Yaml;

class ContentOutputCacheData
{
    private $book;
    private $chapter;
    private $redis;
    private $redis_config;

    public function __construct($book, $chapter, $redis)
    {
        $this->book = $book;
        $this->chapter = $chapter;
        $this->redis = $redis;
        $this->redis_config = Yaml::parse(file_get_contents(__DIR__ . '/../Resources/config/redis_config.yml'));

    }

    /**
     * @param $book_id
     * @return mixed
     * 获得书籍缓存信息
     */
    public function getbookCacheData($book_id)
    {
        $result = $this->setBookInfoAndChapterLIstCache($book_id);
        return $result["book_info"];
    }

    /**
     * @param $book_id
     * @return mixed
     * 获得章节列表缓存信息
     */
    public function getBookChapterListCacheData($book_id)
    {
        $result = $this->setBookInfoAndChapterLIstCache($book_id);
        return $result["book_chapter_list"];
    }

    /**
     * @param $book_id
     * @return array
     * 设置书籍信息和章节列表同时缓存，缓存失效
     */
    private function setBookInfoAndChapterLIstCache($book_id)
    {
        $result = [
            "book_info" => [],
            "book_chapter_list" =>[],
        ];
        $redis_book_info_key = "book_info_v1:book_id:" . $book_id;
        $redis_book_chapter_list_key = "book_chapter_list_v1:book_id:" . $book_id;

        $redis_data_book_info = $this->redis->get($redis_book_info_key);
        if (empty($redis_data_book_info)) {
            $book_info = $this->book->getBookInfo($book_id);
            if(empty($book_info)){//书不存在不缓存
               return  $result;
            }
            $book_chapter_list_arr = $this->chapter->getVolumeAndChapterList($book_id);
            $book_chapter_list=[];
            if($book_chapter_list_arr["code"]=200){
                $book_chapter_list=$book_chapter_list_arr["data"];

                $this->redis->set($redis_book_info_key, json_encode($book_info,
                    JSON_UNESCAPED_UNICODE));
                $this->redis->expire($redis_book_info_key, $this->redis_config["data_expire"]);
                $this->redis->set($redis_book_chapter_list_key,
                    json_encode($book_chapter_list, JSON_UNESCAPED_UNICODE));
                $this->redis->expire($redis_book_chapter_list_key,
                    $this->redis_config["data_expire"]);
            }
            $result = [
                "book_info" => $book_info,
                "book_chapter_list" => $book_chapter_list,
            ];
        } else {
            $redis_data_book_chapter_list = $this->redis->get($redis_book_chapter_list_key);
            $result = [
                "book_info" => json_decode($redis_data_book_info, true),
                "book_chapter_list" => json_decode($redis_data_book_chapter_list, true),
            ];
        }
        return $result;

    }

    /**
     * 获得章节内容缓存
     */
    public function getBookChpaterContentCacheData($book_id, $chapter_id)
    {
        $result=[
            "code"=>404,
            "data"=>[],
            "msg"=>"no data"
        ];
        $redis_key = "book_chapter_content_v1:book_id:" . $book_id . ":chapter_id:" . $chapter_id;
        $redis_chapter_content=$this->redis->get($redis_key);
        if(empty($redis_chapter_content)){
            $chapter_content=  $this->chapter->getBookChapterContent($book_id,$chapter_id);
            if(!empty($chapter_content)){
                $this->redis->set($redis_key,json_encode($chapter_content,true));
                $this->redis->expire($redis_key,$this->redis_config["data_expire"]);
                $result["code"]=200;
                $result["data"]=$chapter_content;
                $result["msg"]="success";
            }

        }else {
            $result["code"] = 200;
            $result["data"] = json_decode($redis_chapter_content, true);
            $result["msg"] = "success";
        }

        return $result;


    }
}